package com.example.mntz.service.Impl;

import com.example.mntz.dao.PostMapper;
import com.example.mntz.model.Cm;
import com.example.mntz.model.LayuiTable;
import com.example.mntz.model.Post;
import com.example.mntz.model.PostBq;
import com.example.mntz.service.PostService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Service
public class PostServiceImpl implements PostService {
    @Autowired
    PostMapper mapper;
//    搜索标签
    @Override
    public List<PostBq> selectbq(PostBq bq) {
        return mapper.selectbq(bq);
    }
//  搜索文章
    @Override
    public LayuiTable selectposts(int limit, int page, String title, String[] bqs ,Integer uid,Integer postStatu,String username) {
        Page page1 = PageHelper.startPage(page, limit);
        List<Post> posts = mapper.selectposts(title,bqs,uid,postStatu,username);
        LayuiTable<Post> post = new LayuiTable(posts);
        post.setCount(page1.getTotal());
        return post;
    }

    @Override
    public Integer selectpostscount(String title, String[] bqss, Integer uid,Integer postStatu) {
        return  mapper.selectpostscount(title,bqss,uid,postStatu);
    }
    @Transactional
    @Override
    public Post selectPost(int postid) {
        mapper.addBrowses(postid);
        return mapper.selectByPrimaryKey(postid);
    }

    @Override
    public LayuiTable<Cm> postcm(int postid, int limit, int page) {
        Page page1 = PageHelper.startPage(page,limit);
        List<Cm> cms = mapper.selectPostCm(postid);
        LayuiTable cmss =new LayuiTable(cms);
        cmss.setCount(page1.getPages());
        return cmss;
    }

    @Override
    public void insterPostcm(Cm cm) {
        mapper.insterPostcm( cm);
    }

    @Override
    public int selectcountcm(Integer postId,Integer userId) {
        return mapper.selectcountcm(postId,userId);
    }

    @Override
    public String insterPostDz(Integer postid, Integer userId) {
        int count =selectcountcm(postid,userId);
        if(count == 0 ){
            mapper.insterPostDz(postid,userId);
            return "already";
        }else{
            mapper.deletePostDz(postid,userId);
            return "cancel";
        }
    }

    @Override
    public String deletePostDz(Integer postid) {
        mapper.deleteByPrimaryKey(postid);
        return "成功";
    }

    @Override
    public void insterPost(Post post) {
        mapper.insterPost(post);
    }

    @Override
    public void updatePost(Post post) {
        mapper.updatePost(post);
    }
}
